﻿USE [HMSIndia]
GO

/****** Object:  UserDefinedFunction [dbo].[fn_GETBillNo]    Script Date: 09/07/2014 09:19:42 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_GETBillNo]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_GETBillNo]
GO

USE [HMSIndia]
GO

/****** Object:  UserDefinedFunction [dbo].[fn_GETBillNo]    Script Date: 09/07/2014 09:19:42 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE FUNCTION [dbo].[fn_GETBillNo]  
(  
 -- Add the parameters for the function here  
 @Year smallint  
)  
RETURNS nvarchar(20)  
AS  
BEGIN  
 -- Declare the return variable here  
 DECLARE @BillNo as nvarchar(20)  
 DECLARE @Prefix as varchar(6)  
 DECLARE @Sufix as varchar(6)  
 DECLARE @RC as bigint  
 SET @BillNo = ''  
 SET @RC = 0  
 SET @Prefix = ''  
 SET @Sufix = ''  
 SELECT  @Prefix = BillPrefix, @RC = BillCrNo , @Sufix = BillSufix FROM dbo.CompanyMaster  

  
 IF @Prefix <> ''  
  SET @BillNo  = @Prefix + '/'  
  
 IF @RC >= 0  
  Begin  
   SET @RC = @RC + 1  
   IF @BillNo <> ''  
    SET @BillNo  =@BillNo  + CAST(@RC as VARCHAR(12))   
   else  
    SET @BillNo  = CAST(@RC as VARCHAR(12))   
  END  
  
 IF @Sufix <> ''  
  SET @BillNo  =@BillNo  + '/' + @Sufix  
  
 IF @RC IS NULL  
  SET @BillNo  = '1001'  
 RETURN @BillNo   
  
END  
GO

